﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using DataMode;
using System.Data;

namespace DAL
{
    public class Album : IAlbum
    {
        DataModeDataContext ctx = new DataModeDataContext();

        public void InsertAlbum(DataMode.video_album album)
        {
            ctx.video_album.InsertOnSubmit(album);
            ctx.SubmitChanges();
        }

        public IOrderedQueryable GetAlbums(string username)
        {
            return (IOrderedQueryable)((from myAlbum in ctx.video_album where myAlbum.album_name == username select myAlbum.album_name));
        }

        public void DeleteAlbum(int id)
        {
            var edit = ctx.video_album.SingleOrDefault<video_album>(s => s.video_album_id  == id);
            ctx.video_album.DeleteOnSubmit(edit);
            ctx.SubmitChanges();
        }

        public video_album findAlbum(int id)
        {
            var edit = ctx.video_album.SingleOrDefault<video_album>(s => s.video_album_id == id);
            ctx.SubmitChanges();
            return edit;
        }

        public void updateAlbum(video_album album)
        {
            var edit = ctx.video_album.SingleOrDefault<video_album>(s => s.video_album_id == album.video_album_id);

            edit.album_name = album.album_name;
            edit.album_description = album.album_description;

            ctx.SubmitChanges();
        }

        public DataMode.video_album GetAlbumInfoByAlbumID(int albumID)
        {
            try
            {
                return ctx.video_album.Single(b => b.video_album_id == albumID);
            }
            catch (InvalidOperationException)
            {
                return null;
            }
        }

        public IOrderedQueryable GetVideosByAlbumID(int albumID)
        {
            return (IOrderedQueryable)(from vd in ctx.video
                                       where vd.video_album_id == albumID
                                       orderby vd.load_time descending
                                       select vd).Take(8);
        }

        public int GetVideosNumByAlbumID(int albumID)
        {
            return (from video in ctx.video
                    where video.video_album_id == albumID
                    select video).Count();
        }


        public void RenameAlbumByAlbumID(int albumID, string albumName)
        {
            DataMode.video_album edit = ctx.video_album.SingleOrDefault<video_album>(s => s.video_album_id == albumID);
            edit.album_name = albumName;
            ctx.SubmitChanges();
            return;
        }


        public IOrderedQueryable GetAlbumsByUsername(string username)
        {
            return (IOrderedQueryable)from alb in ctx.video_album
                   where alb.album_user == username
                   select alb;
        }

        public int GetAlbumIDByAlbumAndUsername(string albumName, string username)
        {
            return ctx.video_album.Single(b => b.album_name == albumName && b.album_user == username).video_album_id;
        }
    }
}
